package com.sync.data.common.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liquanchang
 */
public class ClassUtils {

    public static List<Field> getAllFields(Class<?> clazz) {
        List<Field> fields = new ArrayList<>();

        // 遍历当前类的所有字段
        fields.addAll(Arrays.asList(clazz.getDeclaredFields()));

        // 递归获取父类的所有字段
        Class<?> superclass = clazz.getSuperclass();
        if (superclass != null) {
            fields.addAll(getAllFields(superclass));
        }
        return fields;
    }

    public static Field getFieldByName(Class<?> clazz,String name) {
        List<Field> fields = getAllFields(clazz);
        return fields.stream().filter(s -> (name.equals(s.getName()))).findAny().orElse(null);
    }

}
